from rest_framework.views import APIView
from django.http import HttpResponse
import os,json
from os import listdir
class Index(APIView):
    def post(self, request, *args, **kwargs):
        ip = request.META.get("REMOTE_ADDR")
        new_path = './file/' + ip + '/'
        if not os.path.exists(new_path):
            os.mkdir(new_path)
        myFile = request.FILES.get('file')
        file_name = myFile.name
        with open(new_path + file_name, "w+",encoding='utf-8') as f:
            txt=myFile.read().decode('utf-8').replace("\n","").replace("_","")
            print(txt)
            f.write(txt)
            f.close()
        return HttpResponse(json.dumps({"txt":txt, "ip":ip}), content_type="application/json")
    def put(self, request, *args, **kwargs):
        data = request.query_params
        type = data.get('type')
        ip = data['ip']
        new_path = './file/' + ip + '/'
        if type == 'name':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-PER】')
            label_text.append('【E-PER】')
            label_text.insert(1,'<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    print(content,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'date':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-DATE】')
            label_text.append('【E-DATE】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'company':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-COM】')
            label_text.append('【E-COM】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'money':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-AMT】')
            label_text.append('【E-AMT】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'percentage':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-PCT】')
            label_text.append('【E-PCT】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'location':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-LOC】')
            label_text.append('【E-LOC】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        elif type == 'duty':
            text = data.get('text')
            label_text = list(text)
            label_text.insert(1,'【B-DUTY】')
            label_text.append('【E-DUTY】')
            label_text.insert(1, '<b>')
            label_text.append('</b>')
            label_text = ''.join(label_text)
            for root, dirs, files in os.walk(new_path):
                with open(new_path + files[0], "r",encoding='utf-8') as F:
                    content=F.read().replace(text,label_text)
                    F.close()
                with open(new_path + files[0], "w",encoding='utf-8') as A:
                    A.write(content)
                    A.close()
        else:
            return HttpResponse(status=404,content='请求参数错误')
        return HttpResponse(content)
    def delete(self,request,*args, **kwargs):
        data = request.query_params
        ip = data['ip']
        new_path = './file/' + ip + '/'
        filename = listdir(new_path)
        if data['content'] == 'ok':
            for i in filename:
                c_path = os.path.join(new_path, i)
                os.remove(c_path)
            return HttpResponse('后台文件已删除')
        else:
            return HttpResponse(status=404,content='请求参数错误')
    def get(self,request,*args, **kwargs):
        data = request.query_params
        ip = data['ip']
        new_path = './file/' + ip + '/'
        print(ip,new_path)
        for root, dirs, files in os.walk(new_path):
            file1 = open(new_path + files[0], 'r', encoding='utf-8')
            context = file1.read().replace('<b>', '').replace('</b>', '')
            file1.close()
            file2 = open(new_path + files[0], 'w', encoding='utf-8')
            file2.write(context)
            file2.close()
            file3 = open(new_path + files[0], 'r', encoding='utf-8')
            response = HttpResponse(file3)
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment;filename="{}"'.format(files[0])
            return response